package com.fandou.base.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.fandou.base.R;


/**
 * 通用titleBar
 *
 * @author Jm
 * @date 2019-03-04
 */
public class TitleBar extends RelativeLayout implements View.OnClickListener {
    public static String VIEW_TAG = "TitleBar";

    private ImageView ivLeft;

    private ImageView ivRight;

    private TextView tvCenter;

    private CheckBox cbRight;

    private TextView tvRight;

    private TextView tvLeft;

    private RelativeLayout rlContainer;

    private View vBottomShape;

    private OnLeftClickListener onLeftClickListener;

    public void setOnLeftClickListener(OnLeftClickListener onLeftClickListener) {
        this.onLeftClickListener = onLeftClickListener;
    }

    public void setLeftTextColor(int leftTextColor) {
        tvLeft.setTextColor(leftTextColor);
    }

    public void setOnRightClickListener(OnTvRightClickListener onTvRightClickListener) {
        this.onTvRightClickListener = onTvRightClickListener;
    }

    public void setOnIvRightClickListener(OnIvRightClickListener onIvRightClickListener) {
        this.onIvRightClickListener = onIvRightClickListener;
    }

    private OnTvRightClickListener onTvRightClickListener;

    private OnIvRightClickListener onIvRightClickListener;

    public interface OnLeftClickListener {
        void onToolbarLeftClick();
    }

    public interface OnTvRightClickListener {
        void onTitleBarTvRightClick();
    }

    public interface OnIvRightClickListener {
        void onTitleBarIvRightClickListener();
    }

    public TitleBar(Context context) {
        super(context);
        init(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    View vStatusBarViewHolder;

    public void init(Context context, AttributeSet attrs) {
        //不能删除setTag，用于UI的基类通过findViewByTag找到这个toolbar进行初始化设置
        setTag(VIEW_TAG);
        LayoutInflater.from(context).inflate(R.layout.widget_titlebar, this, true);

        rlContainer = findViewById(R.id.rl_container);
        ivLeft = findViewById(R.id.iv_left);
        ivLeft.setOnClickListener(this);
        tvCenter = findViewById(R.id.tv_center);
        cbRight = findViewById(R.id.cb_right);
        tvRight = findViewById(R.id.tv_right);
        tvRight.setOnClickListener(this);
        tvLeft = findViewById(R.id.tv_left);
        tvLeft.setOnClickListener(this);
        ivRight = findViewById(R.id.iv_right);
        vStatusBarViewHolder = findViewById(R.id.status_bar);
        vBottomShape = findViewById(R.id.v_bottom_shape);
        ivRight.setOnClickListener(this);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);

        boolean isSetStatusBarViewHeight = ta.getBoolean(R.styleable.TitleBar_tb_init_statusbar_view, false);
        if (!isInEditMode()) {
            if (isSetStatusBarViewHeight) {
                ViewGroup.LayoutParams layoutParams = vStatusBarViewHolder.getLayoutParams();

                if (context instanceof Activity) {
                    layoutParams.height = BarUtils.getStatusBarHeight();
                }
                vStatusBarViewHolder.setBackgroundColor(ta.getColor(R.styleable.TitleBar_tb_statusbar_color, Color.parseColor("#FFFFFF")));
            }
        }

        setToolbarBackground(ta.getResourceId(R.styleable.TitleBar_tb_background, -1));

        showBackIcon(ta.getBoolean(R.styleable.TitleBar_tb_show_back_icon, true));
        setTitle(ta.getString(R.styleable.TitleBar_tb_center_title));
        setTitleColor(ta.getColor(R.styleable.TitleBar_tb_center_title_color, getResources().getColor(R.color.black_333333)));
        Drawable drawableRight = ta.getDrawable(R.styleable.TitleBar_tb_center_title_drawable_right);
        if (drawableRight != null) {
            tvCenter.setCompoundDrawablesWithIntrinsicBounds(null, null, drawableRight, null);
            //这里注意，我只需要在drawable不为null的情况下为它设置点击事件。因为这个标题如果没有设置图片，就没必要给他点击事件
            tvCenter.setOnClickListener(this);
        } else {
            tvCenter.setOnClickListener(null);
        }
        setLeftTextColor(ta.getColor(R.styleable.TitleBar_tb_left_text_color, -1));

        Drawable leftDrawable = ta.getDrawable(R.styleable.TitleBar_tb_left_icon);
        if (leftDrawable != null) {
            ivLeft.setImageDrawable(leftDrawable);
        }

        setRightIcon(ta.getResourceId(R.styleable.TitleBar_tb_right_icon, -1));
        showRightIcon(ta.getBoolean(R.styleable.TitleBar_tb_show_right_icon, false));

        setRightText(ta.getString(R.styleable.TitleBar_tb_right_text));
        setRightTextBackground(ta.getResourceId(R.styleable.TitleBar_tb_right_text_background, -1));

        setRightCheckboxText(ta.getString(R.styleable.TitleBar_tb_right_checkbox_text));
        showBottomShape(ta.getBoolean(R.styleable.TitleBar_tb_show_shape, false));
        setRightTextColor(ta.getColor(R.styleable.TitleBar_tb_right_text_color, Color.parseColor("#333333")));


        showRightText(ta.getBoolean(R.styleable.TitleBar_tb_show_right_text, false));


        showRightCheckBox(ta.getBoolean(R.styleable.TitleBar_tb_show_right_checkbox, false));
        setLeftText(ta.getString(R.styleable.TitleBar_tb_left_text));
        ta.recycle();
    }

    private void showBackIcon(boolean show) {
        ivLeft.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * 设置toolbar最左边的icon
     *
     * @param drawable drawableResource
     */
    public void setLeftIcon(int drawable) {
        ivLeft.setImageDrawable(getResources().getDrawable(drawable));
    }

    public void setTitle(CharSequence title) {
        tvCenter.setText(TextUtils.isEmpty(title) ? "" : title);
    }

    /**
     * 设置toolbar中间文字的颜色
     *
     * @param color color res
     */
    public void setTitleColor(int color) {
        tvCenter.setTextColor(color);
    }

    public void setRightText(CharSequence text) {
        if (tvRight.getVisibility() != VISIBLE) {
            tvRight.setVisibility(VISIBLE);
        }
        tvRight.setText(TextUtils.isEmpty(text) ? "" : text);
    }

    /**
     * 设置右边text按钮的背景
     *
     * @param drawableResId drawableResId
     */
    public void setRightTextBackground(int drawableResId) {
        if (drawableResId != -1) {
            tvRight.setBackgroundResource(drawableResId);
        }
    }

    /**
     * 是否显示右边的textView
     *
     * @param show boolean
     */
    public void showRightText(boolean show) {
        tvRight.setVisibility(show ? VISIBLE : GONE);
    }

    public void setRightCheckboxText(String text) {
        cbRight.setText(TextUtils.isEmpty(text) ? "" : text);
    }

    /**
     * 是否显示toolbar底部阴影
     *
     * @param show boolean
     */
    public void showBottomShape(boolean show) {
        vBottomShape.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * 是否显示右边的checkbox
     *
     * @param show show
     */
    public void showRightCheckBox(boolean show) {
        cbRight.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * Set text for toolbar on the right
     *
     * @param text text
     */
    public void setLeftText(String text) {
        tvLeft.setText(TextUtils.isEmpty(text) ? "" : text);
    }

    /**
     * Set color for toolbar on the right
     *
     * @param color color
     */
    public void setRightTextColor(int color) {
        tvRight.setTextColor(color);
    }

    /**
     * 设置toolbar的背景drawableRes
     *
     * @param drawableRes drawableRes
     */
    public void setToolbarBackground(@DrawableRes int drawableRes) {
        if (drawableRes != -1) {
            rlContainer.setBackgroundResource(drawableRes);
            vStatusBarViewHolder.setBackgroundResource(drawableRes);
        }
    }

    /**
     * 是否显示右边的图片按钮
     */
    public void showRightIcon(boolean show) {
        ivRight.setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * 获取右边的imageView
     *
     * @return ImageView
     */
    public ImageView getRightImageView() {
        return ivRight;
    }

    /**
     * 设置右边的图标
     *
     * @param drawableRes DrawableResIds
     */
    public void setRightIcon(@DrawableRes int drawableRes) {
        if (drawableRes != -1) {
            ivRight.setImageResource(drawableRes);
        }
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.iv_left||i==R.id.tv_left) {
            if (onLeftClickListener != null) {
                onLeftClickListener.onToolbarLeftClick();
                return;
            }
            if (getContext() instanceof AppCompatActivity) {
                AppCompatActivity a = (AppCompatActivity) getContext();
                a.onBackPressed();
                KeyboardUtils.hideSoftInput((Activity) getContext());

//                if (a.getSupportFragmentManager().getBackStackEntryCount() > 0) {
//                    Fragment topInStack = FragmentUtils.getTopInStack(a.getSupportFragmentManager());
//                    FragmentUtils.pop(a.getSupportFragmentManager(), false);
//                    KeyboardUtils.hideSoftInput((Activity) getContext());
//
//                } else {
//                    a.onBackPressed();
//                }
            }

        } else if (i == R.id.tv_right) {
            if (onTvRightClickListener != null) {
                onTvRightClickListener.onTitleBarTvRightClick();
                return;
            }

        } else if (i == R.id.iv_right) {
            if (onIvRightClickListener != null) {
                onIvRightClickListener.onTitleBarIvRightClickListener();
                return;
            }

        } else if (i == R.id.tv_center) {//do nothing so far

        } else {
        }
    }
}
